package com.forum.ajax.homepage;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.forum.domain.Remind;
import com.forum.domain.RemindDAO;
import com.forum.domain.Reply;
import com.forum.domain.ReplyDAO;
import com.forum.domain.Topic;
import com.forum.domain.TopicDAO;
import com.forum.domain.User;
import com.forum.domain.UserDAO;

public class AddReply extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public AddReply() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset= utf-8");
		PrintWriter out = response.getWriter();
		Integer userid = Integer.parseInt(request.getParameter("userid"));
		System.out.println("userid:" + userid);
		Integer topicid = Integer.parseInt(request.getParameter("topicid"));
		System.out.println("topicid:" + topicid);
		String atusername = request.getParameter("AtUser");
		System.out.println("AtUser:" + atusername);
		String content = request.getParameter("respond");

		UserDAO userdao = new UserDAO();
		TopicDAO topicdao = new TopicDAO();

		Topic topic = topicdao.findById(topicid);
		System.out.print(topic.toString());

		ReplyDAO replydao = new ReplyDAO();
		RemindDAO reminddao = new RemindDAO();

		User user = userdao.findById(userid);
		User _user = new User();

		if (atusername.equals("")) {
			_user = topic.getUser();
			if (user == _user) {
				System.out.println("zijihuifuziji");
			} else {
				_user = topic.getUser();
			}
			
			System.out.println("in atuername null");
		} else {
			List<User> _userList = userdao.findByProperty("nickname",
					atusername);
			if (_userList.size() == 0) {
				response.sendRedirect("../page/topic.jsp?topicid=" + topicid);
				return;
			} else {
				_user = _userList.get(0);
				System.out.println("yaohuifuderen:"+_user.getNickname());
			}
		}
		Reply reply = new Reply(user, topic, _user, new Timestamp(
				System.currentTimeMillis()), content);

		replydao.save(reply);
		replydao.getSession().beginTransaction().commit();

		Remind remind = new Remind(user, topic, reply, _user);
		reminddao.save(remind);
		reminddao.getSession().beginTransaction().commit();

		topic.setReplycount(topic.getReplycount() + 1);
		response.sendRedirect("../page/topic.jsp?topicid=" + topicid);

	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
